<template>
  <el-dialog
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :title="globalData.title"
    :visible.sync="isDialog"
    width="500px"
  >
    <el-form
      ref="submitFormRef"
      :model="submitForm"
      :rules="submitFormRules"
      label-position="right"
      label-width="auto"
    >
      <el-form-item label="运费" prop="freight">
        <el-input
          v-model="submitForm.freight"
          :min="0"
          clearable
          placeholder="请输入运费"
          type="number"
        ></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button
        :loading="isLoading.submit"
        type="primary"
        @click="handleSubmit"
      >
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { change } from "@/api/order";

export default {
  name: "Edit",
  props: {
    info: {
      type: Object,
    },
  },
  data() {
    return {
      // 表单提交
      submitForm: {
        orderId: "",
        freight: "",
      },
      // 表单验证
      submitFormRules: {
        freight: [{ required: true, message: "请输入运费", trigger: "blur" }],
      },
      // 数据
      globalData: {
        title: "",
      },
      // 加载状态
      isLoading: {
        submit: false,
      },
      // 是否弹窗
      isDialog: true,
    };
  },
  created() {
    this.globalData.title = "编辑运费" + "(" + this.info.order_sn + ")";
    this.submitForm = {
      orderId: this.info.order_id,
      freight: this.info.freight,
    };
  },
  methods: {
    // 提交
    handleSubmit() {
      this.$refs.submitFormRef.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.isLoading.submit = true;

        change(this.submitForm)
          .then(() => {
            this.isLoading.submit = false;
            this.$emit("close");
          })
          .catch(() => {
            this.isLoading.submit = false;
          });
      });
    },
    // 关闭
    handleClose() {
      this.$emit("close", "cancel");
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-textarea textarea {
  font-family: "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
}

::v-deep .tox-tinymce {
  min-height: 500px;
}

.image-box {
  width: 100%;
  height: 100%;
}
</style>
